#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<wait.h>
#include<fcntl.h>
#include<unistd.h>
int main(){
  int pipefd[2];
  if(-1==pipe(pipefd)){
    perror("pipe error!\n");
    return 1;
  }

  pid_t pid = fork();
  if(pid<0){
    perror("fork error!\n");
  }
  else if(pid == 0){
    //child   child进行写操作
    close(pipefd[0]);
    char* msg = "child test write!\n";
    while(1){
      //子进程不断写入
      ssize_t s = write(pipefd[1],msg,strlen(msg));
    }
  }
  else{
    //father   father进行读操作
    close(pipefd[1]);
    char buffer[64];
    int count=0;
    while(1){
      if(count==5){
        //父进程读取五次后关闭读端
        close(pipefd[0]);
        break;
      }
      ssize_t s = read(pipefd[0],buffer,sizeof(buffer)-1);
      count++;
      printf(buffer);
    }
    int status=0;
    //父进程等待子进程获取子进程退出状态
    pid_t ret = waitpid(pid,&status,0);
    //打印子进程收到的信号
    printf("get child signal: %d\n",status & 0x7f);
  }
  return 0;
}
